<template>
  <div class="logistics-company">
    <el-button type="primary" @click="handleAdd">新增物流公司</el-button>
    
    <el-form :inline="true" :model="queryForm" style="margin: 10px 0;">
      <el-form-item label="公司名称">
        <el-input v-model="queryForm.companyName" placeholder="请输入公司名称" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getList">查询</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="companyName" label="公司名称" width="180" />
      <el-table-column prop="minPrice" label="最低费用(分)" width="120" />
      <el-table-column prop="linkUser" label="联系人" width="120" />
      <el-table-column prop="linkPhone" label="联系电话" width="150" />
      <el-table-column prop="sort" label="排序" width="80" />
      <el-table-column prop="isDel" label="状态" width="100">
        <template #default="scope">
          <el-tag v-if="scope.row.isDel == 1" type="success">有效</el-tag>
          <el-tag v-else type="danger">无效</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination 
      @size-change="handleSizeChange" 
      @current-change="handleCurrentChange"
      :current-page="currentPage" 
      :page-sizes="[5, 10, 20]" 
      :page-size="pageSize"
      :total="total" 
      layout="total, sizes, prev, pager, next, jumper"
    />

    <!-- 新增/编辑对话框 -->
    <el-dialog :title="dialogTitle" v-model="dialogVisible" width="500px">
      <el-form :model="form" label-width="100px">
        <el-form-item label="公司名称" prop="companyName" :rules="{ required: true, message: '请输入公司名称', trigger: 'blur' }">
          <el-input v-model="form.companyName" />
        </el-form-item>
        <el-form-item label="最低费用(分)" prop="minPrice" :rules="{ required: true, message: '请输入最低费用', trigger: 'blur' }">
          <el-input v-model.number="form.minPrice" />
        </el-form-item>
        <el-form-item label="联系人" prop="linkUser" :rules="{ required: true, message: '请输入联系人', trigger: 'blur' }">
          <el-input v-model="form.linkUser" />
        </el-form-item>
        <el-form-item label="联系电话" prop="linkPhone" :rules="{ required: true, message: '请输入联系电话', trigger: 'blur' }">
          <el-input v-model="form.linkPhone" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model.number="form.sort" />
        </el-form-item>
        <el-form-item label="状态" prop="isDel">
          <el-select v-model="form.isDel">
            <el-option label="有效" value="1" />
            <el-option label="无效" value="2" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { getCompanyList, addCompany, updateCompany, deleteCompany } from '@/api/logistics'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

const router = useRouter()
const tableData = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const dialogVisible = ref(false)
const dialogTitle = ref('新增物流公司')
const form = reactive({
  id: null,
  companyName: '',
  minPrice: 1000,
  linkUser: '',
  linkPhone: '',
  sort: 1,
  isDel: 1
})
const queryForm = reactive({
  companyName: ''
})

// 获取列表数据
const getList = async () => {
  const res = await getCompanyList({
    page: currentPage.value,
    size: pageSize.value,
    companyName: queryForm.companyName
  })
  tableData.value = res.data.records
  total.value = res.data.total
}

// 分页处理
const handleSizeChange = (val) => {
  pageSize.value = val
  getList()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  getList()
}

// 新增
const handleAdd = () => {
  dialogTitle.value = '新增物流公司'
  Object.assign(form, {
    id: null,
    companyName: '',
    minPrice: 1000,
    linkUser: '',
    linkPhone: '',
    sort: 1,
    isDel: null
  })
  dialogVisible.value = true
}

// 编辑
const handleEdit = (row) => {
  dialogTitle.value = '编辑物流公司'
  Object.assign(form, row)
  dialogVisible.value = true
}

// 提交表单
const submitForm = async () => {
  try {
    if (form.id) {
      await updateCompany(form)
      ElMessage.success('修改成功')
    } else {
      await addCompany(form)
      ElMessage.success('新增成功')
    }
    dialogVisible.value = false
    getList()
  } catch (error) {
    ElMessage.error(error.message)
  }
}

// 删除
const handleDelete = async (row) => {
  try {
    await deleteCompany(row.id)
    ElMessage.success('删除成功')
    getList()
  } catch (error) {
    ElMessage.error(error.message)
  }
}

// 价格管理
const handlePrice = (row) => {
  router.push(`/logistics/price/${row.id}?name=${row.companyName}`)
}

// 重置查询
const resetQuery = () => {
  queryForm.companyName = ''
  getList()
}

// 初始化加载
getList()
</script>